#!/bin/sh

test_description="test execute ref review"

. ./lib/sharness.sh

REPO_ID=""

#创建repo
test_expect_success "[test-1] init repo and branch" '
	REPO_ID=$(find_repo | jq .id)
	if [ -n $REPO_ID ]; then
		delete_repo > delete_repo.resp 2>&1
		test_expect_code 0
	fi
	create_repo > create_repo.resp 2>&1
	REPO_ID=$(cat create_repo.resp) &&
	add_member $WRITE_LEVEL $another_user_external &&
	init_repo > init_repo.resp &&
	test_expect_code 0
'

#创建 master 分支
test_expect_success "[test-2] normal push success" '
	echo "This is the first commit" > "$REPO_DIR"/README &&
	git -C "$REPO_DIR" add README &&
	git -C "$REPO_DIR" commit -m "first commit" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
	git -C "$REPO_DIR" push origin master > test-2 2>&1 &&
	test_expect_code 0
'

test_expect_success "[test-3] execute conflict ref review failed" '
	enable_ref_review $REPO_ID &&
	git -C "$REPO_DIR" push origin HEAD:feat > test-3-1 2>&1 &&
	grep "has been created" test-3-1 &&
	disable_ref_review $REPO_ID &&
	git -C "$REPO_DIR" push origin HEAD:feat > test-3-2 2>&1 &&
	grep "new branch" test-3-2 &&
	execute_ref_review $REPO_ID 1 > execute 2>&1 &&
	grep "该状态下的评审不允许合并" execute
'

test_expect_success "[test-4] execute un-reviewed ref review failed" '
	enable_ref_review $REPO_ID &&
	git -C "$REPO_DIR" push origin HEAD:review > test-4-1 2>&1 &&
	grep "has been created" test-4-1 &&
	execute_ref_review $REPO_ID 2 > execute 2>&1 &&
	grep "该状态下的评审不允许合并" execute
'

test_expect_success "[test-5] execute ref review success" '
	enable_ref_review $REPO_ID &&
	git -C "$REPO_DIR" push origin HEAD:normal > test-5 2>&1 &&
	grep "has been created" test-5 &&
	approve_ref_review $REPO_ID 3 > approve &&
	execute_ref_review $REPO_ID 3 > execute 2>&1 &&
	grep "true" execute
'

test_expect_success "[test-6] check ref review execute result" '
	head=$(git -C "$REPO_DIR" rev-parse HEAD)
	result=$(git -C "$REPO_DIR" ls-remote origin refs/heads/normal 2>&1 | awk "{print \$1}")
	assert $head $result
'

test_expect_success "[test-7] test create complicated ref-review" '
	disable_ref_review $REPO_ID &&
	echo "make commit" >> "$REPO_DIR"/README &&
	git -C "$REPO_DIR" add README &&
	git -C "$REPO_DIR" commit -m "make commit" --author "yunhuai-test <yunhuai.xzy@alibaba-inc.com>" --date=now >/dev/null 2>&1 &&
	git -C "$REPO_DIR" push origin head:master head:to-be-deleted head:to-be-reset head:refs/tags/tag-to-be-deleted head:refs/tags/tag-to-be-reset > test-7-1 2>&1 &&
	enable_ref_review $REPO_ID &&
	git -C "$REPO_DIR" push origin --force head:to-be-created :to-be-deleted head~:to-be-reset :refs/tags/tag-to-be-deleted head~:refs/tags/tag-to-be-reset head:refs/tags/tag-to-be-created > test-7-2 2>&1 &&
	grep "A ref review has been created" test-7-2 &&
	query_ref_review $REPO_ID 4 > query &&
	title=$(cat query | jq .title)
	echo $title | grep "评审请求：新建1个分支 ,删除1个分支 ,重置1个分支 ,创建1个标签 ,删除1个标签 ,重置1个标签"
'

test_expect_success "[test-8] test execute complicated ref-review" '
	approve_ref_review $REPO_ID 4 > approve &&
	execute_ref_review $REPO_ID 4 > execute 2>&1 &&
	grep "true" execute
	head=$(git -C "$REPO_DIR" rev-parse HEAD)
	head_pre=$(git -C "$REPO_DIR" rev-parse HEAD~)
	to_be_created=$(git -C "$REPO_DIR" ls-remote origin refs/heads/to-be-created 2>&1 | awk "{print \$1}")
	assert $head $to_be_created
	to_be_deleted=$(git -C "$REPO_DIR" ls-remote origin refs/heads/to-be-deleted 2>&1)
	assert "" $to_be_deleted
	to_be_reset=$(git -C "$REPO_DIR" ls-remote origin refs/heads/to-be-reset 2>&1 | awk "{print \$1}")
	assert $head_pre $to_be_reset
	tag_to_be_created=$(git -C "$REPO_DIR" ls-remote origin refs/tags/tag-to-be-created 2>&1 | awk "{print \$1}")
	assert $head $tag_to_be_created
	tag_to_be_deleted=$(git -C "$REPO_DIR" ls-remote origin refs/tags/tag-to-be-deleted 2>&1)
	assert "" $to_be_deleted
	tag_to_be_reset=$(git -C "$REPO_DIR" ls-remote origin refs/tags/tag-to-be-reset 2>&1 | awk "{print \$1}")
	assert $head_pre $tag_to_be_reset
'

test_expect_success "[test-7] clean up" '
  delete_repo "$REPO_ID" > test-7 2>&1
'

test_done